/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pgacoordinator;

import java.util.BitSet;
import org.baatar.opt.genetic.AlgorithmParameters;
import org.baatar.opt.genetic.GeneticIterationListener;
import org.baatar.opt.genetic.GeneticOptimizer;
import org.baatar.opt.genetic.chromosome.IChromosome;
import org.baatar.opt.genetic.selection.BinaryTournamentSelection;
import org.baatar.opt.genetic.set.covering.binary.SCChromosomeFactory;
import org.baatar.opt.genetic.set.covering.binary.SCCrossover;
import org.baatar.opt.genetic.set.covering.binary.SCImprover;
import org.baatar.opt.genetic.set.covering.binary.SCMutator;
import org.baatar.opt.genetic.set.meta.DataSet;
import org.baatar.util.XMLUtil;

/**
 *
 * @author Ba
 */
public class SetTest {
    public static void main(String args[]) throws Exception {
        String inputXmlFileName = XMLUtil.readProperty("pgacoordinator.xml", "pgaCoordinator", "inputFile");
        String outputXmlFileName = XMLUtil.readProperty("pgacoordinator.xml", "pgaCoordinator", "outputFile");

//        org.baatar.opt.genetic.set.util.XMLUtil.convertPairXMLtoSetXML(inputXmlFileName, outputXmlFileName);
        DataSet dataSet = org.baatar.opt.genetic.set.util.XMLUtil.readSetOptimizationInputDataFromXML(inputXmlFileName);

        AlgorithmParameters<Boolean, BitSet> algoParams = new AlgorithmParameters<Boolean, BitSet>();

        algoParams.setMaxNumOfIterations(10000);
        algoParams.setMaxNumOfIterationsWOProgress(10000);
        algoParams.setPopulationSize(100);
        algoParams.setMinNumOfChildren(20);
        algoParams.setNumOfEliteChromosomes(4);
        algoParams.setMutationRate((float) (dataSet.rows.length / (2.0 * dataSet.columns.length)));
        algoParams.setGreedRate(0.02f);
        algoParams.setAllowDublicateChromosomes(false);

        algoParams.setChromosomeFactory(new SCChromosomeFactory(dataSet));
        algoParams.setSelector(new BinaryTournamentSelection<Boolean, BitSet>());
        algoParams.setCrossoverOperator(new SCCrossover());
        algoParams.setMutator(new SCMutator(dataSet.columns.length,
                                        dataSet.columns.length / 90.0,
                                        250,
                                        0.8));
        algoParams.setImprover(new SCImprover(dataSet));

        algoParams.getChromosomeFactory().init();

        GeneticOptimizer<Boolean, BitSet> go = new GeneticOptimizer<Boolean, BitSet>();

        go.setPreferences(algoParams);

        go.setGeneticIterationListener(new GeneticIterationListener<Boolean, BitSet>() {
            @Override
            public void onProgress(int optimizerIndex, int iteration, double elapsedTime, String info) {
                try {
                    System.out.println(optimizerIndex + "-" + iteration + "-" + elapsedTime + " sn; " + info);
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
            }

            @Override
            public void onMigrationNeeded(int optimizerIndex, int iteration, IChromosome<Boolean, BitSet> population[]) {
            }

            @Override
            public void onException(int optimizerIndex, String exceptionMessage) {
            }
        });

        if (go.initialize())
            go.start();
    }
}
